from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData
from django.conf import settings

# serializer = Serializer(秘钥, 有效期秒)


# 创建加密方法
def dumps_id_way(openid):
    # 创建serializer对象用于加密
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    data = {'openid': openid}
    # 加密数据
    id = serializer.dumps(data)
    #　返回数据
    return id.decode()


# 创建解密方法
def loads_id_way(id):
    # 创建serializer对象用于加密
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)

    # 解密数据
    try:
        openid = serializer.loads(id)

    except BadData:
        return None
    else:
        return openid.get('openid')
